package com.xxyf;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.json.JsonMapper;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.List;

/**
 * @Author 小小怡飞
 * @Date 2022/9/2 16:39
 * @Version JDK 8
 */
public class Json {
    static JsonMapper json;
    static String sdog;
    static Dog dog;
    static String carJson =
            "{ \"brand\" : \"Mercedes\", \"doors\" : 5," +
                    "  \"owners\" : [\"John\", \"Jack\", \"Jill\"]," +
                    "  \"nestedObject\" : { \"field\" : \"value\" } }";
    static {
        sdog="{\"name\":\"狗\", \"age\":3,\"a\":2}";
        dog=new Dog("狗",3);
        json=new JsonMapper();
    }
    public static void main(String[] args) throws JsonProcessingException {
        Dog dog1 = null;
        json.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false); //忽略多余字符

            dog1 = json.readValue(sdog, Dog.class);

        System.out.println(dog1);
        JsonNode jsonNode= json.readValue(carJson, JsonNode.class);

        JsonNode brand = jsonNode.get("brand");
        JsonNode jsonNode1=null;

           jsonNode1 = json.readTree(sdog);

        Dog dog = json.treeToValue(jsonNode1, Dog.class);
        System.out.println(dog);

        String s = brand.asText();


    }
}
@Data
@AllArgsConstructor
@NoArgsConstructor
class Dog implements Serializable {
    private String name;
    private int age;
}